//
//  SkinUpdateInterface.m
//  CPICApp
//
//  Created by zhiwei li on 13-3-27.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "SkinUpdateInterface.h"
#import "Config.h"
#import "RCAlertViewUtil.h"
#import "NSString+SBJson.h"
#import "NSObject+SBJson.h"
#import "ViewController.h"
#import "AppDelegate.h"

#define IF_SKIN_UPDATE @"IF_SKIN_UPDATE"

@implementation SkinUpdateInterface
@synthesize progress;

-(void)setSkinUpdateRequest//向太保移动平台发送重大节日皮肤自动更新接口请求报文
{
//    NSString *strRequestCode=@"RequestCode=62015";
    NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
    [dicRequest setObject:@"admin" forKey:@"userAccount"];
    [dicRequest setObject:@"" forKey:@"versioncode"];
    NSString *strRequestJson=[dicRequest JSONRepresentation];
//    strRequestJson=[NSString stringWithFormat:@"&RequestData=%@",strRequestJson];
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestJson];
//    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrlEncod];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"请求报文为:%@",strUrl);
     requestSkin= [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [requestSkin setPostValue:@"62015" forKey:@"RequestCode"];
    [requestSkin setPostValue:strRequestJson forKey:@"RequestData"];
    
    [requestSkin setDelegate:self];
    [requestSkin setDidFailSelector:@selector(ResponseFail:)];
    [requestSkin setDidFinishSelector:@selector(ResonseSuccess:)];
    [requestSkin startAsynchronous];
    
    showProgress = YES;
    if (showProgress) 
    {
        progress= [[MBProgressHUD alloc]initWithView:self];
        [self addSubview:progress];
        [self bringSubviewToFront:progress];
        progress.labelText = @"加载中...";
        [progress show:NO];
        showProgress = NO;
        [progress setHidden:YES];
    };
}

- (void)ResonseSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    
    NSMutableDictionary *jsonoSubObj = [dictResponse objectForKey:@"head"];
    NSString * ss = [[NSString alloc] initWithFormat:@"%@",[jsonoSubObj objectForKey:@"success"]];
    if ([ss isEqualToString:@"1"])
    {
        DicthemeStyleSetting = [dictResponse objectForKey:@"themeStyleSettingList"];
        ArraythemeStyleSetting=[DicthemeStyleSetting objectForKey:@"data"];
        NSLog(@"-------------%@",ArraythemeStyleSetting);
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"有新的皮肤可更新！" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
        [alertView show];
        alertView.delegate = self;
        [alertView release];
    }
    else
    {
        //[self justShowMessage:@"暂无新的皮肤可更新！" title:@"提示"];
    }
}

- (void)ResponseFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败" title:@"注意"];
}

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(alert.tag)
    {
        case 0:
            if (buttonIndex == [alert cancelButtonIndex])
            {
                NSString * ifupdate = @"0";
                NSUserDefaults *accountDefaults1 = [NSUserDefaults standardUserDefaults];
                [accountDefaults1 setObject:ifupdate forKey:@"IF_SKIN_UPDATE"];
                [accountDefaults1 synchronize];
                
                ViewController * home = [[ViewController alloc]init];
                [AppDelegate app].unc =nil;
                [AppDelegate app].unc = [[UINavigationController alloc] initWithRootViewController:home];
                [[AppDelegate app].window addSubview:[AppDelegate app].unc.view];
                [[AppDelegate app].window makeKeyAndVisible];
            }
            else
            {
                //默认cancelButtonIndex = 0，每个按钮index可设
                {
                    for (int k = 0; k < [ArraythemeStyleSetting count]; k++)
                    {
                        NSMutableDictionary * dicskin = [ArraythemeStyleSetting objectAtIndex:k];
                        NSString * stylePicName=[[NSString alloc] initWithFormat:@"%@",[dicskin objectForKey:@"stylePicName"]];
                        
                        if ([stylePicName isEqualToString:@"main_bg.png"])
                        {
                            //NSString * stylePicId = @"4028818f3da54bed013da58e6ec00004";
                            NSString *strMainURL=[[NSString alloc]initWithFormat:@"%@",[dicskin objectForKey:@"stylePicId"]];
                            stylePicNameUrl=[NSString stringWithFormat:@"%@%@",Image_Url,strMainURL];
                            stylePicNameUrl = [stylePicNameUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
                            [[NSUserDefaults standardUserDefaults] setObject:stylePicNameUrl forKey:SKIN_IMG_URL_01];
                            [strMainURL release];
                        }
                        else if([stylePicName isEqualToString:@"back.png"])
                        {
                            //NSString * stylePicId = @"4028818f3da54bed013da58de1e80002";
                            NSString *strBackURL=[[NSString alloc]initWithFormat:@"%@",[dicskin objectForKey:@"stylePicId"]];
                            stylePicNameUrl=[NSString stringWithFormat:@"%@%@",Image_Url,strBackURL];
                            stylePicNameUrl = [stylePicNameUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
                            [[NSUserDefaults standardUserDefaults] setObject:stylePicNameUrl forKey:SKIN_IMG_URL_02];
                            [strBackURL release];
                        }
                        else if([stylePicName isEqualToString:@"icon.png"])
                        {
                            //NSString * stylePicId = @"4028818f3da54bed013da58e44c80003";
                            NSString *strIconURL=[[NSString alloc]initWithFormat:@"%@",[dicskin objectForKey:@"stylePicId"]];
                            stylePicNameUrl=[NSString stringWithFormat:@"%@%@",Image_Url,strIconURL];
                            stylePicNameUrl = [stylePicNameUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
                            [[NSUserDefaults standardUserDefaults] setObject:stylePicNameUrl forKey:SKIN_IMG_URL_03];
                            [strIconURL release];
                        }
                        else if([stylePicName isEqualToString:@"bg.png"])
                        {
                            //NSString * stylePicId = @"4028818f3da54bed013da58ce1ef0001";
                            NSString *strBackgroundURL=[[NSString alloc]initWithFormat:@"%@",[dicskin objectForKey:@"stylePicId"]];
                            stylePicNameUrl=[NSString stringWithFormat:@"%@%@",Image_Url,strBackgroundURL];
                            stylePicNameUrl = [stylePicNameUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
                            [[NSUserDefaults standardUserDefaults] setObject:stylePicNameUrl forKey:SKIN_IMG_URL_04];
                            [strBackgroundURL release];
                        }
                    }
                    NSString * appbgtype = @"重大节日皮肤";
                    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
                    [accountDefaults setObject:appbgtype forKey:@"UserDefaultBgKindKey"];
                    
                    NSString * ifupdate = @"0";
                    NSUserDefaults *accountDefaults1 = [NSUserDefaults standardUserDefaults];
                    [accountDefaults1 setObject:ifupdate forKey:@"IF_SKIN_UPDATE"];
                    [accountDefaults1 synchronize];
                    
                    ViewController * home = [[ViewController alloc]init];
                    [AppDelegate app].unc =nil;
                    [AppDelegate app].unc = [[UINavigationController alloc] initWithRootViewController:home];
                    [[AppDelegate app].window addSubview:[AppDelegate app].unc.view];
                    [[AppDelegate app].window makeKeyAndVisible];
                }
            }

            break;
        default:
            break;
    }
}

-(void)dealloc
{
    requestSkin.delegate=nil;
    [super dealloc];
}
@end
